package com.message.tcp.ip.socket.configure.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.message.tcp.ip.socket.configure.entity.DataElement;
import com.message.tcp.ip.socket.util.SqliteConnection;
@Repository("DataElementDao")
public class DataElementDaoImp implements DataElementDao {
	
	@Override
	public List<DataElement> getDataValue() throws SQLException {
		String sql = "SELECT DENUM, DATAVAL, DEFINITION FROM DATAELEMENT";
		Connection conn = SqliteConnection.getConnection();
		PreparedStatement stat = conn.prepareStatement(sql);
		ResultSet rs = stat.executeQuery();
		List<DataElement> list = new ArrayList<DataElement>();
		while(rs.next()){
			String deNum = rs.getString("DENUM");
			String dataVal = rs.getString("DATAVAL");
			String def = rs.getString("DEFINITION");
			DataElement de = new DataElement();
			de.setDeNum(deNum);
			de.setDataVal(dataVal);
			de.setDef(def);
			list.add(de);
		}
		return list;
	}

	@Override
	public void save(DataElement de) throws SQLException {
		String sql = "INSERT INTO DATAELEMENT (DENUM, DATAVAL, DEFINITION) VALUES ( ?, ?, ?)";
		Connection conn = SqliteConnection.getConnection();
		PreparedStatement stat = conn.prepareStatement(sql);
		stat.setString(1, de.getDeNum());
		stat.setString(2, de.getDataVal());
		stat.setString(3, de.getDef());
		stat.execute();
	}

	@Override
	public DataElement getDataElement(DataElement de) throws SQLException {
		String sql = "SELECT DENUM, DATAVAL, DEFINITION FROM DATAELEMENT WHERE DENUM = ?";
		Connection conn = SqliteConnection.getConnection();
		PreparedStatement stat = conn.prepareStatement(sql);
		stat.setString(1, de.getDeNum());
		ResultSet rs = stat.executeQuery();
		DataElement d = new DataElement();
		while(rs.next()){
			String deNu = rs.getString("DENUM");
			String dataVal = rs.getString("DATAVAL");
			String def = rs.getString("DEFINITION");
			d.setDeNum(deNu);
			d.setDataVal(dataVal);
			d.setDef(def);
		}
		return d;
	}

	@Override
	public void update(DataElement de) throws SQLException {
		String sql = "UPDATE DATAELEMENT SET DATAVAL = ?, DEFINITION = ? WHERE DENUM = ?";
		Connection conn = SqliteConnection.getConnection();
		PreparedStatement stat = conn.prepareStatement(sql);
		stat.setString(1,de.getDataVal());
		stat.setString(2, de.getDef());
		stat.setString(3, de.getDeNum());
		stat.execute();
	}

	@Override
	public String getDataValue(String deNum) throws SQLException {
		String sql = "SELECT DATAVAL AS VAL FROM DATAELEMENT WHERE DENUM = ?";
		Connection conn = SqliteConnection.getConnection();
		PreparedStatement stat = conn.prepareStatement(sql);
		stat.setString(1, deNum);
		ResultSet rs = stat.executeQuery();
		String dataVal = "";
		while(rs.next()){
			dataVal = rs.getString("VAL");
		}
		return dataVal;
	}

}
